package ewms.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import ewms.bo.EwmsEntryWarehouseAddBo;
import ewms.bo.EwmsEntryWarehouseDetailAddBo;
import ewms.bo.EwmsEntryWarehouseEditBo;
import ewms.bo.EwmsEntryWarehouseQueryBo;
import ewms.domain.EwmsEntryWarehouse;
import ewms.domain.EwmsEntryWarehouseDetail;
import ewms.domain.EwmsWasteCollectDetail;
import ewms.domain.EwmsWasteInventory;
import ewms.mapper.EwmsEntryWarehouseDetailMapper;
import ewms.mapper.EwmsEntryWarehouseMapper;
import ewms.mapper.EwmsWasteCollectDetailMapper;
import ewms.mapper.EwmsWasteInventoryMapper;
import ewms.utils.EwmsUtils;
import ewms.vo.EwmsEntryWarehouseVo;
import ewms.common.core.page.PagePlus;
import ewms.common.core.page.TableDataInfo;
import ewms.common.exception.CustomException;
import ewms.common.utils.PageUtils;
import ewms.domain.EwmsWasteDetail;
import ewms.mapper.EwmsWasteDetailMapper;
import ewms.service.IEwmsEntryWarehouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.validation.constraints.NotEmpty;
import java.util.*;

/**
 * 危废入库Service业务层处理
 *
 * @author zhaoxiaochun
 * @date 2022-02-09
 */
@Service
public class EwmsEntryWarehouseServiceImpl extends ServiceImpl<EwmsEntryWarehouseMapper, EwmsEntryWarehouse> implements IEwmsEntryWarehouseService {

	@Autowired
	private EwmsWasteCollectDetailMapper wasteCollectDetailMapper;
	@Autowired
	private EwmsEntryWarehouseDetailMapper entryWarehouseDetailMapper;
	@Autowired
	private EwmsWasteInventoryMapper wasteInventoryMapper;
	@Autowired
	private EwmsWasteDetailMapper wasteDetailMapper;

	@Override
	public EwmsEntryWarehouseVo queryById(Long entryWarehouseId) {
		return getVoById(entryWarehouseId, EwmsEntryWarehouseVo.class);
	}

	@Override
	public TableDataInfo<EwmsEntryWarehouseVo> selectPagePreEntryWarehouseList(EwmsEntryWarehouseQueryBo bo) {
		return PageUtils.buildDataInfo(baseMapper.selectPagePreEntryWarehouseList(PageUtils.buildPage(), bo));
	}

	@Override
	public List<EwmsEntryWarehouseVo> selectPreEntryWarehouseList(EwmsEntryWarehouseQueryBo bo) {
		return baseMapper.selectPreEntryWarehouseList(bo);
	}

	@Override
	public TableDataInfo<EwmsEntryWarehouseVo> selectPageEntryWarehouseList(EwmsEntryWarehouseQueryBo bo) {
		return PageUtils.buildDataInfo(baseMapper.selectPageEntryWarehouseList(PageUtils.buildPage(), bo));
	}

	@Override
	public List<EwmsEntryWarehouseVo> selectEntryWarehouseList(EwmsEntryWarehouseQueryBo bo) {
		return baseMapper.selectEntryWarehouseList(bo);
	}

	@Override
	public TableDataInfo<EwmsEntryWarehouseVo> queryPageList(EwmsEntryWarehouseQueryBo bo) {
		PagePlus<EwmsEntryWarehouse, EwmsEntryWarehouseVo> result = pageVo(PageUtils.buildPagePlus(), buildQueryWrapper(bo), EwmsEntryWarehouseVo.class);
		return PageUtils.buildDataInfo(result);
	}

	@Override
	public List<EwmsEntryWarehouseVo> queryList(EwmsEntryWarehouseQueryBo bo) {
		return listVo(buildQueryWrapper(bo), EwmsEntryWarehouseVo.class);
	}

	private LambdaQueryWrapper<EwmsEntryWarehouse> buildQueryWrapper(EwmsEntryWarehouseQueryBo bo) {
		Map<String, Object> params = bo.getParams();
		LambdaQueryWrapper<EwmsEntryWarehouse> lqw = Wrappers.lambdaQuery();
		lqw.eq(bo.getWarehouseId() != null, EwmsEntryWarehouse::getWarehouseId, bo.getWarehouseId());
		lqw.between(params.get("beginEntryDate") != null && params.get("endEntryDate") != null,
			EwmsEntryWarehouse::getEntryDate, params.get("beginEntryDate"), params.get("endEntryDate"));
		return lqw;
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public Boolean insertByAddBo(EwmsEntryWarehouseAddBo bo) {
		boolean saveFlag = false;
		// 保存入库主表信息
		EwmsEntryWarehouse add = BeanUtil.toBean(bo, EwmsEntryWarehouse.class);
		add.setEntryDate(EwmsUtils.handleSelectDate(add.getEntryDate()));
		validEntityBeforeSave(add);
		saveFlag = save(add);
//		List<@NotEmpty(message = "危废收运明细id不能为空") Long> wasteCollectDetailList = Arrays.asList(bo.getWasteCollectDetailIds());
		@NotEmpty(message = "危废入库明细信息不能为空") List<EwmsEntryWarehouseDetailAddBo> entryWarehouseDetailList = bo.getEntryWarehouseDetailList();
		for (EwmsEntryWarehouseDetailAddBo entryWarehouseDetail : entryWarehouseDetailList) {
			EwmsWasteCollectDetail wasteCollectDetail = wasteCollectDetailMapper.selectById(entryWarehouseDetail.getWasteCollectDetailId());
			if (Objects.isNull(wasteCollectDetail)) {
				throw new CustomException("危废收运信息未找到");
			}

			// 保存入库明细信息
			EwmsEntryWarehouseDetail addDetail = new EwmsEntryWarehouseDetail();
			addDetail
				.setEntryWarehouseId(add.getEntryWarehouseId())
				.setCollectDetailId(entryWarehouseDetail.getWasteCollectDetailId())
				.setCreateTime(add.getCreateTime())
				.setCreateBy(add.getCreateBy());
			saveFlag = saveFlag && SqlHelper.retBool(entryWarehouseDetailMapper.insert(addDetail));

			// 保存库存信息
			EwmsWasteDetail wasteDetail = wasteDetailMapper.selectWasteDetailById(wasteCollectDetail.getWasteId());
			if (Objects.isNull(wasteDetail)) {
				throw new CustomException("危废详细信息未找到");
			}
			EwmsWasteInventory ewmsWasteInventory = new EwmsWasteInventory();
			ewmsWasteInventory
				.setWasteId(wasteCollectDetail.getWasteId())
				.setWasteName(wasteDetail.getWasteName())
				.setBatchNumber(wasteCollectDetail.getBatchNumber())
				.setWarehouseId(bo.getWarehouseId())
				.setEntryWarehouseDetailId(addDetail.getEntryWarehouseDetailId())
				.setWasteWeight(entryWarehouseDetail.getWasteWeight())
				.setCreateBy(add.getCreateBy())
				.setCreateTime(add.getCreateTime());
			saveFlag = saveFlag && SqlHelper.retBool(wasteInventoryMapper.insert(ewmsWasteInventory));
		}
		return saveFlag;
	}

	@Override
	public Boolean updateByEditBo(EwmsEntryWarehouseEditBo bo) {
		EwmsEntryWarehouse update = BeanUtil.toBean(bo, EwmsEntryWarehouse.class);
		validEntityBeforeSave(update);
		return updateById(update);
	}

	/**
	 * 保存前的数据校验
	 *
	 * @param entity 实体类数据
	 */
	private void validEntityBeforeSave(EwmsEntryWarehouse entity) {
		//TODO 做一些数据校验,如唯一约束
	}

	@Override
	public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
		if (isValid) {
			//TODO 做一些业务上的校验,判断是否需要校验
		}
		return removeByIds(ids);
	}
}
